#include "open_power_code.h"
#include "SC_Init.h"	// MCU initialization header file, including all firmware library header files
#include "SC_it.h"
#include "..\Drivers\SCDriver_list.h"
#include "HeadFiles\SysFunVarDefine.h"
#include <SC95F8x7x_C.H>
#include "..\FWLib\SC95F_Lib\inc\sc95f_timer1.h"
#include "machine_stat.h"
#include "lcd_comm.h"
OPENPOWER_CODE_STRUCT openpower_code;
//uint8_t open_power_code_flag=0;
uint8_t code key[9]={11,19,13,23,17,29,11,13,17};
uint8_t openpower_flag=0;
uint8_t openpower_x_flash=0;

//uint8_t open_power_code_arr[6]={0};
void open_power_times(void)
{
#ifdef OPEN_POWER_FLAG
    openpower_flag++;
    if(openpower_flag>=200)
    {
        openpower_flag=0;
    }
#endif
}

void open_power_code_process(void)
{
#ifdef OPEN_POWER_FLAG
   // uint16_t openpower_code.offset,n0,n1,n2;
    if(openpower_code.open_power_success==OPEN_POWER_NONE || openpower_code.open_power_success==OPEN_POWER_TEMP)
    {
        if(openpower_code.open_power_code_flag==1)//aaron,��������룬openpower_flagͨ���ж�interrupt 3ѭ����0-200�Լӣ�����������ʱ��ȡopenpower_flag��ֵͨ������������������������
        {
            openpower_code.Seed=openpower_flag%100;
            // TIM1_ITConfig(DISABLE,LOW);
			// TIM1_Cmd(DISABLE);
            if(openpower_x_flash==0)
            {
                openpower_code.Seed_x=(openpower_code.master_code[2]*100)+openpower_code.Seed;
            }
            openpower_x_flash=1;
            openpower_code.Seed_y=(openpower_code.Seed_x*6)+182;
            openpower_code.Seed_r=openpower_code.Seed_y%10000;
            while(openpower_code.Seed_r<999)
            {
                openpower_code.Seed_x=(((openpower_code.Seed_r%100)*100)+openpower_code.Seed+1)%9999;
                openpower_code.Seed_y=(openpower_code.Seed_x*6)+182;
                openpower_code.Seed_r=openpower_code.Seed_y%9999;
            }
            openpower_code.Seed_x=(((openpower_code.Seed_r%100)*100)+openpower_code.Seed+1)%9999;
            openpower_code.suiji_code[2]=(openpower_code.Seed_r-(openpower_code.Seed_r%100))/100;
            openpower_code.suiji_code[1]=openpower_code.Seed_r%100;
            openpower_code.suiji_code[0]=openpower_code.Seed;
            
            //aaron�����������������ͬ��������������
            if((openpower_code.suiji_code[2]==openpower_code.suiji_code[1])||
               (openpower_code.suiji_code[2]==openpower_code.suiji_code[0])||
               (openpower_code.suiji_code[1]==openpower_code.suiji_code[0])){
                openpower_code.open_power_code_flag=1;
                openpower_code.input_code[0]=0;
                openpower_code.input_code[1]=0;
                openpower_code.input_code[2]=0;
                return;
            }
            // openpower_code.suiji_code[2]=;
            // openpower_code.suiji_code[1]=;
            // openpower_code.suiji_code[0]=;
            openpower_code.offset=(openpower_code.master_code[0]+openpower_code.master_code[1]+openpower_code.master_code[2]+(3*(openpower_code.suiji_code[0]+openpower_code.suiji_code[1]+openpower_code.suiji_code[2])))%10;
            // n2 =(openpower_code.suiji_code[2] + key[(openpower_code.offset + 5)% 10])^(openpower_code.master_code[2] + key[(openpower_code.offset + 0 % 10)]);
            // n1 =(openpower_code.suiji_code[1] + key[(openpower_code.offset + 4)% 10])^(openpower_code.master_code[1] + key[(openpower_code.offset + 1)% 10]);
            // n0 =(openpower_code.suiji_code[0] + key[(openpower_code.offset + 3)% 10])^(openpower_code.master_code[0] + key[(openpower_code.offset + 2)% 10]);
            openpower_code.n_array[2] =(openpower_code.master_code[2] + key[(openpower_code.offset + 5)% 9])^(openpower_code.suiji_code[2] + key[(openpower_code.offset + 0)% 9]);
            openpower_code.n_array[1] =(openpower_code.master_code[1] + key[(openpower_code.offset + 4)% 9])^(openpower_code.suiji_code[1] + key[(openpower_code.offset + 1)% 9]);
            openpower_code.n_array[0] =(openpower_code.master_code[0] + key[(openpower_code.offset + 3)% 9])^(openpower_code.suiji_code[0] + key[(openpower_code.offset + 2)% 9]);
            openpower_code.openpower_code_array[2]=((openpower_code.n_array[0] * key[0])+ (openpower_code.n_array[1] * key[8])+ (openpower_code.n_array[2] * key[5]))% 100;
            openpower_code.openpower_code_array[1]=((openpower_code.n_array[0] * key[2])+ (openpower_code.n_array[1] * key[6])+ (openpower_code.n_array[2] * key[3]))% 100;
            openpower_code.openpower_code_array[0]=((openpower_code.n_array[0] * key[4])+ (openpower_code.n_array[1] * key[1])+ (openpower_code.n_array[2] * key[7]))% 100;
            openpower_code.open_power_code_flag=2;
            
            
        }
        else if(openpower_code.open_power_code_flag==6)//�û�����
        {
            if((openpower_code.input_code[0]==openpower_code.openpower_code_array[2])\
            &&(openpower_code.input_code[1]==openpower_code.openpower_code_array[1])\
            &&(openpower_code.input_code[2]==openpower_code.openpower_code_array[0]))
            {
                openpower_code.open_power_success = OPEN_POWER_OK;
                TIM1_ITConfig(DISABLE,LOW);
                TIM1_Cmd(DISABLE);
            }else if(((openpower_code.input_code[0]==66)\
				        &&(openpower_code.input_code[1]==66)\
                        &&(openpower_code.input_code[2]==66))){
                openpower_code.input_code[0]=0;
                openpower_code.input_code[1]=0;
                openpower_code.input_code[2]=0;            
                openpower_code.open_power_success = OPEN_POWER_TEMP;            
            }else
            {

                display_reset_openpowercode_index();
            }
        }
    }
#endif
}

void OpenPower_Reset(void){
    openpower_code.input_code[0] = 0;
	openpower_code.input_code[1] = 0;
	openpower_code.input_code[2] = 0;
    display_reset_openpowercode_index();
}
